package com.zhiyou.check.service.impl;

import com.zhiyou.check.dao.RestDao;
import com.zhiyou.check.dao.SingnInDao;
import com.zhiyou.check.pojo.Rest;
import com.zhiyou.check.pojo.SingnIn;
import com.zhiyou.check.service.RestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.List;

@Service
public class RestServiceImpl implements RestService {

    @Autowired
    RestDao restDao;
    @Autowired
    SingnInDao singnInDao;


    @Override
    public List<Rest> findByTidAndByRState(Integer tid, int rState, Integer rType) {
        if(rState==0){
            return  restDao.findByTidAndByRState0(tid,rType);
        }
        return restDao.findByTidAndByRState1(tid,rType);
    }

    @Override
    public List<Rest> findBySidAndByRState(Integer sid, int rState, Integer rType) {
        if(rState==0){
            return  restDao.findBySidAndByRState0(sid,rType);
        }
        return restDao.findBySidAndByRState1(sid,rType);
    }

    // 1: 已同意,0 未同意
    public void updateRState(Rest rest) {
        if (rest.getrState()==1){
            // 查询考勤表中是否有请假学生当天的出勤记录 参数： 请假开始时间
            SingnIn s= singnInDao.findByStartTime(rest.getStartTime());
            if (s==null){ //学生请假时如果没有天出勤记录,则添加当天考勤记录！！
                singnInDao.insertRestSingnIn(new SingnIn(rest.getSid(),rest.getGid(),rest.getGname(),new SimpleDateFormat("YYYY-MM-dd").format(rest.getStartTime())));
            }else {//学生请假通过时,修改请假开始后,当天出勤记录为请假
                singnInDao.updateRest(rest.getSid(),rest.getStartTime());
            }
        }
        restDao.updateRState(rest);
    }

    @Override
    public void insertRest(Rest rest) {
        restDao.insertRest(rest);
    }
}
